<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>popot-2.01: /opt/GoogleCode/popot/trunk/popot-2.01/src/maths.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">popot-2.01
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">/opt/GoogleCode/popot/trunk/popot-2.01/src/maths.h</div>  </div>
</div>
<div class="contents">
<a href="maths_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef POPOT_MATH_H</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define POPOT_MATH_H</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;math.h&gt;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="comment">//Warning:</span>
<a name="l00008"></a>00008 <span class="comment">// Before inclusion of this file, it is mandatory to use define the random number generator</span>
<a name="l00009"></a>00009 <span class="comment">// e.g.  #define popot::rng::KissRNG RNG_GENERATOR</span>
<a name="l00010"></a>00010 
<a name="l00011"></a>00011 <span class="keyword">namespace </span>popot
<a name="l00012"></a>00012 {
<a name="l00013"></a><a class="code" href="namespacepopot_1_1math.html">00013</a>   <span class="keyword">namespace </span>math
<a name="l00014"></a>00014   {
<a name="l00015"></a>00015 
<a name="l00020"></a><a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">00020</a>     <span class="keywordtype">double</span> <a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(<span class="keywordtype">double</span> min, <span class="keywordtype">double</span> max)
<a name="l00021"></a>00021     {
<a name="l00022"></a>00022       <span class="keywordflow">return</span> min + (max - min) * <span class="keywordtype">double</span>(RNG_GENERATOR::rng_rand()) / RNG_GENERATOR::RNG_RAND_MAX;
<a name="l00023"></a>00023     }
<a name="l00024"></a>00024 
<a name="l00029"></a><a class="code" href="namespacepopot_1_1math.html#a9c65d115b061817f7d4b26f685e1b774">00029</a>     <span class="keywordtype">double</span> <a class="code" href="namespacepopot_1_1math.html#a9c65d115b061817f7d4b26f685e1b774">uniform_random_exclusive</a>(<span class="keywordtype">double</span> min, <span class="keywordtype">double</span> max)
<a name="l00030"></a>00030     {
<a name="l00031"></a>00031       <span class="keywordflow">return</span> min + (max - min) * <span class="keywordtype">double</span>(RNG_GENERATOR::rng_rand()) / (RNG_GENERATOR::RNG_RAND_MAX+1);
<a name="l00032"></a>00032     }
<a name="l00033"></a>00033 
<a name="l00037"></a><a class="code" href="namespacepopot_1_1math.html#a379cb433fc4a216e75252d7278c161a7">00037</a>     <span class="keywordtype">double</span> <a class="code" href="namespacepopot_1_1math.html#a379cb433fc4a216e75252d7278c161a7">normal</a>(<span class="keywordtype">double</span> mean, <span class="keywordtype">double</span> std)
<a name="l00038"></a>00038     {
<a name="l00039"></a>00039       <span class="comment">// We generate a sample following a normal distribution</span>
<a name="l00040"></a>00040       <span class="comment">// with the Box-Muller method</span>
<a name="l00041"></a>00041       <span class="comment">// u and v are randomly generated in ] 0 ; 1]</span>
<a name="l00042"></a>00042       
<a name="l00043"></a>00043       <span class="keywordtype">double</span> u = 1.0 - <a class="code" href="namespacepopot_1_1math.html#a9c65d115b061817f7d4b26f685e1b774">uniform_random_exclusive</a>(0,1);
<a name="l00044"></a>00044       <span class="keywordtype">double</span> v = 1.0 - <a class="code" href="namespacepopot_1_1math.html#a9c65d115b061817f7d4b26f685e1b774">uniform_random_exclusive</a>(0,1);
<a name="l00045"></a>00045       <span class="keywordtype">double</span> x = sqrt(-2.0 * log(u)) * cos(2.0 * M_PI * v);
<a name="l00046"></a>00046       
<a name="l00047"></a>00047       <span class="keywordflow">return</span> mean + std * x;
<a name="l00048"></a>00048     }
<a name="l00049"></a>00049 
<a name="l00050"></a><a class="code" href="namespacepopot_1_1math.html#a490555fab3908934d1318c3b35c1568a">00050</a>     <span class="keywordtype">double</span> <a class="code" href="namespacepopot_1_1math.html#a490555fab3908934d1318c3b35c1568a">normal_spso2011</a>(<span class="keywordtype">double</span> mean, <span class="keywordtype">double</span> std)
<a name="l00051"></a>00051     {
<a name="l00052"></a>00052       <span class="comment">// We generate a sample following a normal distribution</span>
<a name="l00053"></a>00053       <span class="comment">// with the Box-Muller method</span>
<a name="l00054"></a>00054       <span class="comment">// u and v are randomly generated in ] 0 ; 1]</span>
<a name="l00055"></a>00055       <span class="keywordtype">double</span> x1,x2,w, y1;
<a name="l00056"></a>00056       <span class="keywordflow">do</span>
<a name="l00057"></a>00057         {
<a name="l00058"></a>00058           x1 = 2.0 * <a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(0,1) - 1.0;
<a name="l00059"></a>00059           x2 = 2.0 * <a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(0,1) - 1.0;
<a name="l00060"></a>00060           w = x1*x1+x2*x2;
<a name="l00061"></a>00061         } 
<a name="l00062"></a>00062       <span class="keywordflow">while</span>(w &gt;= 1.0);
<a name="l00063"></a>00063 
<a name="l00064"></a>00064       w = sqrt (-2.0 * log (w) / w);
<a name="l00065"></a>00065       y1 = x1 * w;
<a name="l00066"></a>00066       
<a name="l00067"></a>00067       <span class="keywordflow">if</span>(<a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(0,1)&lt;0.5) 
<a name="l00068"></a>00068         y1=-y1; 
<a name="l00069"></a>00069       y1 = y1 * std + mean;
<a name="l00070"></a>00070       <span class="keywordflow">return</span> y1;  
<a name="l00071"></a>00071     }
<a name="l00072"></a>00072 
<a name="l00077"></a><a class="code" href="namespacepopot_1_1math.html#aa928311374a0ce22928a61a324b53fac">00077</a>     <span class="keywordtype">int</span> <a class="code" href="namespacepopot_1_1math.html#aa928311374a0ce22928a61a324b53fac">uniform_integer</a>(<span class="keywordtype">int</span> imin, <span class="keywordtype">int</span> imax)
<a name="l00078"></a>00078     {
<a name="l00079"></a>00079       <span class="keywordflow">return</span> floor(<a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(<span class="keywordtype">double</span>(imin), <span class="keywordtype">double</span>(imax+1)));
<a name="l00080"></a>00080     }
<a name="l00081"></a>00081 
<a name="l00086"></a><a class="code" href="namespacepopot_1_1math.html#a66e3abf5e0c3bce08adae996d492937b">00086</a>     <span class="keywordtype">int</span> <a class="code" href="namespacepopot_1_1math.html#a66e3abf5e0c3bce08adae996d492937b">random_from_array</a>(<span class="keywordtype">double</span>* proba)
<a name="l00087"></a>00087     {
<a name="l00088"></a>00088       <span class="keywordtype">double</span> val = <a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">uniform_random</a>(0.0, 1.0);
<a name="l00089"></a>00089       <span class="keywordtype">int</span> i = 0;
<a name="l00090"></a>00090       <span class="keywordtype">double</span> sum = proba[0];
<a name="l00091"></a>00091       <span class="keywordflow">while</span>(sum &lt; val)
<a name="l00092"></a>00092         {
<a name="l00093"></a>00093           i++;
<a name="l00094"></a>00094           sum += proba[i];
<a name="l00095"></a>00095         }
<a name="l00096"></a>00096       <span class="keywordflow">return</span> i;
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098 
<a name="l00102"></a><a class="code" href="namespacepopot_1_1math.html#aa4604e8bc29a36d50df7fb804cfc8fef">00102</a>     <span class="keywordtype">int</span> <a class="code" href="namespacepopot_1_1math.html#aa4604e8bc29a36d50df7fb804cfc8fef">random_gibbs_from_array</a>(<span class="keywordtype">double</span> * proba, <span class="keywordtype">int</span> nb, <span class="keywordtype">double</span> inv_temperature)
<a name="l00103"></a>00103     {
<a name="l00104"></a>00104       <span class="keywordtype">double</span> exp_proba[nb];
<a name="l00105"></a>00105       <span class="keywordtype">double</span> sum = 0.0;
<a name="l00106"></a>00106       <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0 ; i &lt; nb ; ++i)
<a name="l00107"></a>00107         {
<a name="l00108"></a>00108           exp_proba[i] = exp(inv_temperature * proba[i]);
<a name="l00109"></a>00109           sum += exp_proba[i];
<a name="l00110"></a>00110         }
<a name="l00111"></a>00111       <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0 ; i &lt; nb ; ++i)
<a name="l00112"></a>00112         exp_proba[i] /= sum;
<a name="l00113"></a>00113       <span class="keywordflow">return</span> <a class="code" href="namespacepopot_1_1math.html#a66e3abf5e0c3bce08adae996d492937b">random_from_array</a>(exp_proba);
<a name="l00114"></a>00114     }
<a name="l00115"></a>00115 
<a name="l00120"></a><a class="code" href="namespacepopot_1_1math.html#a4b05877ed8e34acd5435021422a4d2a6">00120</a>     <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1math.html#a4b05877ed8e34acd5435021422a4d2a6">random_shuffle_indexes</a>(<span class="keywordtype">size_t</span> * indexes, <span class="keywordtype">size_t</span> S)
<a name="l00121"></a>00121     {
<a name="l00122"></a>00122       <span class="keywordtype">size_t</span> * index_temp = <span class="keyword">new</span> <span class="keywordtype">size_t</span>[S];
<a name="l00123"></a>00123       <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; S ; ++i)
<a name="l00124"></a>00124         index_temp[i] = i;
<a name="l00125"></a>00125 
<a name="l00126"></a>00126       <span class="keywordtype">size_t</span> rank;
<a name="l00127"></a>00127       <span class="keywordtype">size_t</span> length = S;
<a name="l00128"></a>00128       <span class="keywordtype">size_t</span> i ,t;
<a name="l00129"></a>00129       <span class="keywordflow">for</span> (i=0; i &lt; S; ++i)
<a name="l00130"></a>00130         {
<a name="l00131"></a>00131           rank=<a class="code" href="namespacepopot_1_1math.html#aa928311374a0ce22928a61a324b53fac">uniform_integer</a>(0,length-1);
<a name="l00132"></a>00132           indexes[i]=index_temp[rank];
<a name="l00133"></a>00133           
<a name="l00134"></a>00134           <span class="keywordflow">if</span> (rank&lt;length-1)    <span class="comment">// Compact</span>
<a name="l00135"></a>00135             {
<a name="l00136"></a>00136               <span class="keywordflow">for</span> (t=rank; t&lt;length-1; ++t)
<a name="l00137"></a>00137                 index_temp[t]=index_temp[t+1];
<a name="l00138"></a>00138             }                                   
<a name="l00139"></a>00139           length=length-1;
<a name="l00140"></a>00140         }
<a name="l00141"></a>00141       <span class="keyword">delete</span>[] index_temp;
<a name="l00142"></a>00142     }
<a name="l00143"></a>00143 
<a name="l00144"></a>00144   }<span class="comment">// namespace math</span>
<a name="l00145"></a>00145 } <span class="comment">// namespace popot</span>
<a name="l00146"></a>00146 
<a name="l00147"></a>00147 <span class="preprocessor">#endif // POPOT_MATH_H</span>
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5
</small></address>

</body>
</html>
